package com.CityHunter.Activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.CityHunter.R;
import com.CityHunter.DataObject.User;
import com.CityHunter.Request.LoginRequest;
import com.CityHunter.Request.UpdateStateRequest;
import com.CityHunter.Service.UpdateLocationService;

public class LoginPage extends Activity {

	// Declare buttons
	private Button sttp_btn_login;
	private Button sttp_btn_register;

	// Declare EditTexts
	private EditText sttp_et_email;
	private EditText sttp_et_password;
	
	private SharedPreferences sp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sttp);
		sp = getSharedPreferences("cityhunter", 0);
		
		// Mapping buttons
		sttp_btn_login = (Button) findViewById(R.id.sttp_btn_login);
		sttp_btn_register = (Button) findViewById(R.id.sttp_btn_register);

		// Mapping EditTexts
		sttp_et_email = (EditText) findViewById(R.id.sttp_et_email);
		sttp_et_password = (EditText) findViewById(R.id.sttp_et_password);

		// Set Listener for buttons
		sttp_btn_login.setOnClickListener(handleLogin);
		sttp_btn_register.setOnClickListener(handleRegister);

		// Set Listener for EditTexts
		sttp_et_email.setOnFocusChangeListener(checkEmailFormat);
	}

	@Override
	public void onDestroy(){
		sp = getSharedPreferences("cityhunter", 0);
		// Get userid from bundle
		int userid = sp.getInt("userid", 0);
		super.onDestroy();
	}
	
	// Implement button login on click listener
	private OnClickListener handleLogin = new OnClickListener() {
		public void onClick(View v) {

			String email = sttp_et_email.getText().toString();
			String password = sttp_et_password.getText().toString();
			int result = 0;

			String loginPage = getString(R.string.url_loginPage);

			LoginRequest lr = new LoginRequest(loginPage);
			User user = lr.login(email,password);
			
			boolean isValidUser = (user.getUserid() == 0) ? false : true;

			// Handle Login
			if (isValidUser) {
				
				sp.edit().putInt("userid", user.getUserid()).commit();
				sp.edit().putString("email", user.getEmail()).commit();
				sp.edit().putString("name", user.getName()).commit();
				
				// Update User State
				String updateStatePage = getString(R.string.url_updateState);
				
				UpdateStateRequest usr = new UpdateStateRequest(updateStatePage);
				usr.updateState(user.getUserid(),1);

				Log.d("Sttp_Debug", "Login Successful");
				
				// Create Service
				Intent i = new Intent(LoginPage.this, UpdateLocationService.class);
				startService(i);
				
				i = new Intent(LoginPage.this, WelcomePage.class);
				startActivity(i);

			} else {
				Toast.makeText(LoginPage.this,
						"Wrong email or password. \nPlease try again.",
						Toast.LENGTH_LONG).show();
			}

		}
	};

	// Implement button register on click listener
	private OnClickListener handleRegister = new OnClickListener() {
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(LoginPage.this, RegisterPage.class);
			startActivity(intent);
		}
	};

	// Implement edit text email focus change listener
	private OnFocusChangeListener checkEmailFormat = new OnFocusChangeListener() {
		@Override
		public void onFocusChange(View v, boolean hasFocus) {

			EditText sttp_et_email = (EditText) findViewById(R.id.sttp_et_email);
			String email = sttp_et_email.getText().toString();

			// If focus changed
			if (!hasFocus) {
				// If email format is incorrect
				if (!email.endsWith(".com") && email.indexOf("@") < 0) {
					Toast.makeText(LoginPage.this, "E-mail format is incorrect!",
							Toast.LENGTH_LONG).show();
				}
			}
		}
	};

}
